package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.payment.config.AlipayConfig;
import com.spzx.payment.service.AliPayService;
import com.spzx.rabbit.constant.MqConst;
import com.spzx.rabbit.service.RabbitService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class AliPayServiceImpl implements AliPayService {

    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2hBC6CNlAgcXGNBmnGI8SQp08BVaEohcjL0sHDnPdUrcBvsfKdCAS3Q29Ch8EZp+TLhm3JgsoTDRIaDTvYiPupDMXIXEhfQaqOzUnT3Dr6mSk/RdTHtQIwlU0x9pE9Eb8IBIMW0E/Dc7O+qBb9SdGsOIq2zcpWYCLlVSEakmbx255iBWHHLOATWgAqMMsQ/E5Cc94nf7iFZuoxWP4DOu9nEws61XpsLCdmtrYiset0m6FmiHCZUdx/+29rGJBPPOkzs0p7G0TswTJBw/rKy3pk2wOZzEVOGC+ybOBHe3LQL0f/F5h9MRUu5u5ZnJSFjZRze730uCpIK3s39f3ZMdfAgMBAAECggEAWwRc+wjynlKY+BB/Qm7i2i5SDt5ymM3kT+2Dss8oRJS9IqerJnDmlcGa2kyzzxQGO/VfaZAVPFFUOU+OJlqApL9Jox1QN20L59SC20mofF11NznnadIT1xJiP2HynTRcOdscZQEU9obCPUGKupD0tzH6d3i+d2Zkzjb34NjIQGBHdWJTqOHLsBjR0Fq56wcg8zhiOi2ZKKiM1KW7iIH5A628GQ/mA1/Aa1pTR0lFyPATQkpbEsrOuwpbfD+qg9l/Efu/c/idblv5UxYJYRuczhvG/nej6yk1H//53zgMQ5QQgieu+5FaUe0U1iD5+p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VZU+Dy7I6zzHPowxD1DWLgPqwU2zUGku61omut0+E43cvTkBb2bq+U+1hL63xUrkDo0X1jNctcxgpsPRCfomGY5f51YzyXfZmf6uDnmpZAUrYqX8l+wcOSZujJXFfvH7cCH1ezdOmTXmFAfGODtckwSv8yNt5sX1v6V0uRCLDzS2HuTxdGanbs4K/uq5X06KX1NhxJYLlL6IY9xGcODT0aIglIwIDAQAB";

    @Autowired
    AlipayConfig alipayConfig;

    @Autowired
    RabbitService rabbitService;

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {

        // 初始化SDK，client是http的发送工具公共参数
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
                "9021000126676072",
                privateKey,
                "JSON",
                "utf-8",
                publicAliKey,
                "RSA2"
        );

        // 构造请求参数以调用接口，封装参数，请求参数
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        map.put("product_code","QUICK_WAP_WAY");
        map.put("total_amount","0.01");
        map.put("subject","测试手机");
        request.setBizContent(JSON.toJSONString(map));
        request.setNotifyUrl("http://nnu8bp.natappfree.cc/payment/alipay/callback/notify");
        request.setReturnUrl("http://192.168.222.30/#/pages/money/paySuccess");

        // 调用api
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        String form = response.getBody();// 生成form表单
        System.out.println("打印支付宝form表单：");
        System.out.println(form);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        // 处理业务
        // TODO
        System.out.println("生成payment支付信息");
        return form;
    }

    @SneakyThrows
    @Override
    public String notify(Map<String, String> paramMap) {
        String alipay_public_key = AlipayConfig.alipay_public_key;
        String charset = AlipayConfig.charset;

        // 验签
        boolean b = AlipaySignature.rsaCheckV1(paramMap, publicAliKey, charset, "RSA2");

        if(b){
            String alipay_url = alipayConfig.getAlipay_url();
            String app_private_key = alipayConfig.getApp_private_key();
            String app_id = alipayConfig.getApp_id();

            String notify_payment_url = AlipayConfig.notify_payment_url;
            String return_payment_url = AlipayConfig.return_payment_url;
            String format = AlipayConfig.format;
            System.out.println(alipay_url);
            System.out.println(app_private_key);
            System.out.println(app_id);
            System.out.println(alipay_public_key);
            System.out.println(notify_payment_url);
            System.out.println(return_payment_url);
            System.out.println(charset);
            System.out.println(format);

            // 初始化SDK，client是http的发送工具公共参数
            AlipayClient alipayClient = new DefaultAlipayClient(
                    alipay_url,
                    app_id,
                    app_private_key,
                    format,
                    charset,
                    alipay_public_key,
                    "RSA2"
            );

            // 处理业务
            // TODO
            System.out.println("更新payment支付信息");
            // 通知订单
            String out_trade_no = paramMap.get("out_trade_no");
            rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,out_trade_no);

            return "success";
        }else {
            System.out.println("验签失败");
            return "fail";
        }

    }
}
